import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { useWalletStore } from '../stores/walletStore'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login', // 独立的登录页面
      name: 'login',
      component: ()=> import('../views/LoginView.vue')
    },
    {
      path: '/', // 主页作为父路由
      component: ()=> import('../views/DashboardView.vue'),
      children: [
        {
          path: '', // 默认子路由（主页内容）
          name: 'home',
          component: ()=> import('../views/HomeView.vue')
        },
        {
          path: 'world',
          name: 'world',
          component: ()=> import('../views/WorldView.vue')
        },
        {
          path: 'market',
          name: 'market',
          component: ()=> import('../views/MarketView.vue')
        },
        {
          path: 'inventory',
          name: 'inventory',
          component: ()=> import('../views/InventoryView.vue')
        },
        {
          path: 'social',
          name: 'social',
          component: ()=> import('../views/SocialView.vue')
        },
        {
          path: 'collection',
          name: 'collection',
          component: ()=> import('../views/CollectionView.vue')
        }
      ]
    }
  ],
})
// 全局前置守卫
router.beforeEach((to, from, next) => {
  const walletStore = useWalletStore()

  // 如果未连接钱包，且访问的不是登录页，则跳转到 /login
  if (!walletStore.isConnected && to.path !== '/login') {
    next('/login')
  } else {
    next() // 继续访问目标页面
  }
})
export default router
